package com.sparrow.common.findpath;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.function.Consumer;

public class MapFrame extends JFrame {

    public MapPanel mapPanel;
    private JButton play;
    private JButton block;
    private JButton start;
    private JButton end;

    private int clickMode = 0;

    private Consumer<MapFrame> consumer;

    public void addFindPathConsumer(Consumer<MapFrame> consumer) {
        this.consumer = consumer;
    }

    public MapFrame(byte[][] mapData) {
        setLayout(new BorderLayout());
        mapPanel = new MapPanel(mapData);
        mapPanel.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                SwingUtilities.invokeLater(() -> {
                    int x = e.getX();
                    int y = e.getY();
                    switch (clickMode) {
                        case 0: {
                            mapPanel.markStart(x, y);
                            clickMode = 3;
                            break;
                        }
                        case 1: {
                            mapPanel.markBlock(x, y);
                            break;
                        }
                        case 2: {
                            mapPanel.markStart(x, y);
                            break;
                        }
                        case 3: {
                            mapPanel.markEnd(x, y);
                        }
                    }
                    mapPanel.repaint();
                });
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        add(new JLabel("地图"), BorderLayout.NORTH);
        add(mapPanel, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MapFrame frame = this;
        play = new JButton("寻路");
        play.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                consumer.accept(frame);
                mapPanel.play();
            }
        });

        block = new JButton("不可走");
        block.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clickMode = 1;
            }
        });

        start = new JButton("起点");
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clickMode = 2;
            }
        });

        end = new JButton("终点");
        end.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clickMode = 3;
            }
        });

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(play);
        buttonPanel.add(block);
        buttonPanel.add(start);
        buttonPanel.add(end);
        add(buttonPanel, BorderLayout.SOUTH);
        // 获取屏幕尺寸
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        // 计算窗口的初始位置，使其居中
        setLocation((screenSize.width - mapData.length * MapPanel.tileSize) / 2,
                (screenSize.height - mapData[0].length * MapPanel.tileSize - 100) / 2);
        pack();
        setVisible(true);

        mapPanel.requestFocus();
    }


    @Override
    public void paintComponents(Graphics g) {
        super.paintComponents(g);
    }

    public void addPath(int[] pos) {
        mapPanel.paths.add(pos);
    }

    public void setPath(List<int[]> path) {
        mapPanel.paths = path;
    }

    public int[] getEnd() {
        return mapPanel.end;
    }


    public int[] getStart() {
        return mapPanel.start;
    }
}
